package liu.de.service;

import liu.de.dto.Bean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static liu.de.config.RabbitMqConfig.EXCHANGE_DELAY;
import static liu.de.config.RabbitMqConfig.EXCHANGE_TEST;

/**
 * @author liude
 */
@SuppressWarnings("WeakerAccess")
@Service
@Slf4j
public class MqService {
    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public MqService(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMessage(Bean data) {
        rabbitTemplate.convertAndSend(EXCHANGE_TEST, "", data);
    }

    public void sendDelayMessage(Bean data, int delayedInSeconds) {

        MessagePostProcessor delayedPostProcessor = message -> {
            message.getMessageProperties().setDelay(delayedInSeconds * 1000);
            return message;
        };

        rabbitTemplate.convertAndSend(EXCHANGE_DELAY, "", data, delayedPostProcessor);
    }

}
